# application/api/system/device.py
from flask import Blueprint, jsonify, request
from application.services.system.device import DeviceService
from application.utils.res import json_fail, json_success, page_success


# 创建蓝图
bp = Blueprint("device", __name__, url_prefix="/device")


@bp.get("/list")
def get_device_list():
    """
    分页获取设备列表
    支持条件查询：pageNum, pageSize, deviceName, deviceModel, manufacturer, deviceStatus, communicationType
    """
    try:
        # 获取分页参数
        page_num = int(request.args.get("pageNum", 1))
        page_size = int(request.args.get("pageSize", 10))

        # 获取查询条件
        conditions = {
            "deviceName": request.args.get("deviceName"),
            "deviceModel": request.args.get("deviceModel"),
            "manufacturer": request.args.get("manufacturer"),
            "deviceStatus": request.args.get("deviceStatus"),
            "communicationType": request.args.get("communicationType"),
        }

        # 过滤空值条件
        filtered_conditions = {
            k: v for k, v in conditions.items() if v is not None and v != ""
        }

        # 调用服务获取数据
        device_list, total = DeviceService.get_device_list(
            page_num, page_size, filtered_conditions
        )

        return page_success(device_list, total)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500


@bp.post("")
def create_device():
    """
    创建设备
    """
    try:
        # 获取请求数据
        data = request.get_json()

        if not data:
            return json_fail("请求数据不能为空"), 400

        result = DeviceService.create_device(data)
        return json_success(result, "创建成功", 200)
    except Exception as e:
        return json_fail(f"创建失败: {str(e)}"), 500


@bp.put("")
def update_device():
    """
    更新设备
    :param device_id: 设备ID
    """
    try:
        # 获取请求数据
        data = request.get_json()

        if not data:
            return json_fail("请求数据不能为空"), 400

        device_id = data.get("deviceId")
        if not device_id:
            return json_fail("设备ID不能为空"), 400

        result = DeviceService.update_device(device_id, data)
        if result:
            return json_success(result, "更新成功")
        else:
            return json_fail("设备不存在"), 404
    except Exception as e:
        return json_fail(f"更新失败: {str(e)}"), 500


@bp.delete("/<int:device_id>")
def delete_device(device_id):
    """
    删除设备
    :param device_id: 设备ID
    """
    try:
        result = DeviceService.delete_device(device_id)
        if result:
            return json_success(None, "删除成功")
        else:
            return json_fail("设备不存在"), 404
    except Exception as e:
        return json_fail(f"删除失败: {str(e)}"), 500


@bp.get("/optionselect")
def get_device_optionselect():
    """
    获取设备下拉列表
    """
    try:
        result = DeviceService.get_device_optionselect()
        return json_success(result)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500
    

@bp.get("/test")
def test_device():
    """
    测试设备通讯
    """
    try:
        data = request.get_json()
        result = DeviceService.test_device(data)
        return json_success(result)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500
    